#include "VEnginePCH.h"
#include "VEngine.h"
#include "VCallback.h"

extern void ShutdownStringTable();
#ifdef VPLATFORM_WIN32
BOOL VEngine::EditorMode = FALSE;
#endif 
VRender* VEngine::Render = NULL;
VFrameAllocator* VEngine::MainThreadFrameAllocator = NULL;
VUISystem* VEngine::GUI = NULL;
VSpriteCache* VEngine::SpriteCache = NULL;
VScene*	VEngine::Scene = NULL;
BOOL VEngine::Init()
{
	MainThreadFrameAllocator = new VFrameAllocator;
	if (MainThreadFrameAllocator == NULL)
	{
		return FALSE;
	}
	MainThreadFrameAllocator->Init(1024);
	
	// init rtti
	if(!VRTTObject::StaticInit())
	{
		return FALSE;
	}

	// create render 
	Render = VRender::CreateRender();
	if (Render== NULL)
	{
		Shutdown();
		return FALSE;
	}
	
	SpriteCache = new VSpriteCache;
	if (SpriteCache == NULL)
	{
		Shutdown();
		return FALSE;
	}
	
	Scene = new VScene;


	// create gui system
	GUI = new VUISystem;
	if (!GUI || !GUI->Initialize())
	{
		VLOG("Initialize gui failed!");
		Shutdown();
		return FALSE;
	}
	
	return TRUE;
}

void VEngine::Shutdown()
{
	
	if (GUI)
	{
		GUI->Shutdown();
		delete GUI;
		GUI = NULL;
	}
	if (Scene)
	{
		delete Scene;
	}
	VScene::StaticShutdown();

	if (SpriteCache)
	{
		delete SpriteCache;
		SpriteCache = NULL;
	}
	if (Render)
	{
		Render->Release();
		Render = NULL;
	}
	VRTTObject::StaticShutdown();
	
	if (MainThreadFrameAllocator)
	{
		delete MainThreadFrameAllocator;
	}
	
	ShutdownStringTable();
	VLOG("Venus shutdown successfully!");
}